"""
    @Author  ：思念 
    @File    ：1、芒果TV电影信息.py
    @Date    ：2024/12/17 20:56 
"""
import requests
import pymongo
import redis
import hashlib


class MangoTV:
    def __init__(self):
        self.client = pymongo.MongoClient("mongodb://localhost:27017/")
        self.db = self.client["py_spider"]["mango_movie_list"]
        self.redis_client = redis.Redis()
        # try:
        #     self.db.drop()  # 使用 drop 方法删除集合
        #     print("删除成功")
        # except Exception as e:
        #     print(f"删除失败: {e}")
        self.api_url = 'https://pianku.api.mgtv.com/rider/list/pcweb/v3'
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
        }

    def get_movie_info(self, params_attrs):
        result_json = requests.get(self.api_url, params=params_attrs, headers=self.headers).json()
        return result_json

    def parse_item(self, response):
        movie_list = response["data"]["hitDocs"]
        for movie in movie_list:
            item = dict()
            item['title'] = movie['title']
            item['subtitle'] = movie['subtitle']
            item['story'] = movie['story']
            self.save_item(item)

    @staticmethod
    def get_MD5(response):
        # md5方法接收的参数必须是bytes（字节）类型，所以需要先将字符串转换为bytes类型
        m = hashlib.md5(str(response).encode('utf-8')).hexdigest()
        return m

    def save_item(self, item):
        md5 = self.get_MD5(item)
        # sadd 方法返回的是添加成功的元素个数，如果元素已经存在，返回0
        flag = self.redis_client.sadd("movie:filter", md5)
        if flag:
            try:
                self.db.insert_one(item)
                print("插入成功：", item)
            except Exception as e:
                print(f"插入失败: {e}")
        else:
            print("数据重复")

    def close_spider(self):
        self.client.close()
        self.redis_client.close()
        print("爬虫结束")

    def main(self):
        for page_num in range(1, 11):
            params = {
                "allowedRC": "1",
                "platform": "pcweb",
                "channelId": "2",
                "pn": page_num,
                "pc": "80",
                "hudong": "1",
                "_support": "10000000",
                "kind": "19",
                "area": "10",
                "year": "all",
                "chargeInfo": "a1",
                "sort": "c2",
                "feature": "all"
            }
            result_dict = self.get_movie_info(params)
            self.parse_item(result_dict)
        mango.close_spider()


if __name__ == '__main__':
    mango = MangoTV()
    mango.main()
